package guoanmaker.provider.business.service;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;

import javax.annotation.Resource;
import javax.transaction.Transactional;

import org.springframework.stereotype.Service;

import guoanmaker.provider.business.dao.ProvideSpaceReserveDao;
import guoanmaker.provider.business.model.PersonalUser;
import guoanmaker.provider.business.model.ProvideRoom;
import guoanmaker.provider.business.model.ProvideSpaceReserve;
import guoanmaker.provider.business.model.ReturnResult;
import guoanmaker.provider.business.modelnobind.SpaceReserve;
import guoanmaker.provider.business.repository.PersonalUserRepository;
import guoanmaker.provider.business.repository.ProvideRoomRepository;
import guoanmaker.provider.business.repository.ProvideSpaceReserveRepository;
import guoanmaker.provider.helper.common.util.SystemHelper;

/**
 * 
* @ClassName: ProvideSpaceReserveService 
* @Description: 预约空间service
* @author Yvonne_MGL
* @date 2017年8月15日 上午11:00:31 
*  
 */
@Service
public class ProvideSpaceReserveService {
	@Resource
	private ProvideSpaceReserveDao provideSpaceReserveDao;
	
	@Resource
	private ProvideSpaceReserveRepository provideSpaceReserveRepository;
	
	@Resource
	private ProvideRoomRepository provideRoomRepository;
	
	@Resource
	private PersonalUserRepository personalUserRepository;
	
	/**
	 * 
	* @Description: 根据条件查询预约空间 
	* @param beginTime 参观起始时间
	* @param endTime 参观结束时间
	* @param likeStr 关键字：模糊查询 联系人，空间名，备注信息
	* @param state 0为未预约  1为已预约
	* @param pageNumber 页数
	* @param pageSize 每页条数
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月15日 上午11:03:46     
	* @return Map    
	* @throws 
	 */
	public Map findByProperties(String beginTime,String endTime,String likeStr,String state,String businessid ,Integer pageNumber, Integer pageSize,String userid){
		return provideSpaceReserveDao.findByProperties(beginTime, endTime, likeStr, state,businessid, pageNumber, pageSize,userid);
	}
	
	/**
	 * 
	* @Description: 根据id查询预约参观详情 
	* @param id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年10月14日 上午11:13:00     
	* @return SpaceReserve    
	* @throws 
	 */
	public SpaceReserve findSpaceReserveById(String id){
		return provideSpaceReserveDao.findSpaceReserveById(id);
	}
	
	/**
	 * 
	* @Description: 完成预约空间 
	* @param id 预约空间服务id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月15日 上午11:17:05     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult overReserve(String id){
		ReturnResult returnResult=new ReturnResult();
		ProvideSpaceReserve provideSpaceReserve=provideSpaceReserveRepository.findOne(id);
		if(provideSpaceReserve==null){
			returnResult.setKey("error");
			returnResult.setValue("预约空间服务不存在");
		}else{
			provideSpaceReserve.setState("1");
			provideSpaceReserveRepository.save(provideSpaceReserve);
			returnResult.setKey("success");
    		returnResult.setValue("完成预约成功");
		}
		return returnResult;
	}
	
	/**
	 * 
	* @Description: 申请预约参观 
	* @param reserveTime 参观时间
	* @param contactPerson 联系人
	* @param contactNumber 联系电话
	* @param reserveAmount 参观人数
	* @param remark 备注
	* @param roomid 空间id
	* @param @return
	* @author Yvonne_MGL 
	* @date 2017年8月15日 下午3:07:49     
	* @return ReturnResult    
	* @throws 
	 */
	@Transactional
	public ReturnResult createProvideSpaceReserve(String reserveTime,String contactPerson,String contactNumber,int reserveAmount,String remark,String roomid,String userid)throws Exception{
		ReturnResult returnResult=new ReturnResult();
		ProvideRoom room=provideRoomRepository.findOne(roomid);
		PersonalUser user=personalUserRepository.findOne(userid);
		SimpleDateFormat sdf =   new SimpleDateFormat( "yyyy-MM-dd" );
		if(room==null){
			returnResult.setKey("error");
			returnResult.setValue("空间不存在");
		}else if(user==null){
			returnResult.setKey("error");
			returnResult.setValue("用户不存在");
		}else{
			ProvideSpaceReserve provideSpaceReserve=new ProvideSpaceReserve();
			provideSpaceReserve.setCreateTime(new Date());
			provideSpaceReserve.setRoom(room);
			provideSpaceReserve.setReserveTime(sdf.parse(reserveTime));
			provideSpaceReserve.setContactPerson(contactPerson);
			provideSpaceReserve.setContactNumber(contactNumber);
			provideSpaceReserve.setReserveAmount(reserveAmount);
			provideSpaceReserve.setRemark(remark);
			provideSpaceReserve.setState("0");
			provideSpaceReserve.setUser(user);
			provideSpaceReserveRepository.save(provideSpaceReserve);
			returnResult.setKey("success");
    		returnResult.setValue("申请预约成功");
    		
    		String telephone=room.getBusiness().getLicensPhone();
    		String message="【国安创客】尊敬的服务商用户您好，有新用户预约空间，请您与用户及时进行沟通确认。预约详情请登录服务商端查看。感谢您对“国安创客”平台的支持与信任。";
    		SystemHelper.sendMessage(telephone, message);
		}
		return returnResult;
	}

}
